package com.powerbank.devicecenter.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.powerbank.devicecenter.entity.Station;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

/**
 * 充电站Mapper接口
 */
@Mapper
public interface StationMapper extends BaseMapper<Station> {

    /**
     * 根据站点编号查询站点
     */
    @Select("SELECT * FROM station WHERE station_code = #{stationCode} AND deleted = 0")
    Station findByStationCode(@Param("stationCode") String stationCode);

    /**
     * 根据城市查询站点列表
     */
    @Select("SELECT * FROM station WHERE city = #{city} AND deleted = 0")
    List<Station> findByCity(@Param("city") String city);

    /**
     * 根据状态查询站点列表
     */
    @Select("SELECT * FROM station WHERE status = #{status} AND deleted = 0")
    List<Station> findByStatus(@Param("status") String status);

    /**
     * 查询活跃站点列表
     */
    @Select("SELECT * FROM station WHERE status = 'ACTIVE' AND enabled = 1 AND deleted = 0")
    List<Station> findActiveStations();

    /**
     * 根据位置范围查询站点
     */
    @Select("SELECT * FROM station WHERE " +
            "longitude BETWEEN #{minLng} AND #{maxLng} " +
            "AND latitude BETWEEN #{minLat} AND #{maxLat} " +
            "AND status = 'ACTIVE' AND enabled = 1 AND deleted = 0")
    List<Station> findByLocationRange(@Param("minLng") BigDecimal minLng,
                                    @Param("maxLng") BigDecimal maxLng,
                                    @Param("minLat") BigDecimal minLat,
                                    @Param("maxLat") BigDecimal maxLat);

    /**
     * 更新站点设备统计
     */
    @Update("UPDATE station SET " +
            "available_count = #{availableCount}, " +
            "occupied_count = #{occupiedCount}, " +
            "fault_count = #{faultCount}, " +
            "updated_time = NOW() " +
            "WHERE id = #{stationId}")
    int updateDeviceCount(@Param("stationId") Long stationId,
                         @Param("availableCount") Integer availableCount,
                         @Param("occupiedCount") Integer occupiedCount,
                         @Param("faultCount") Integer faultCount);

    /**
     * 增加站点服务次数
     */
    @Update("UPDATE station SET total_service_count = total_service_count + 1, updated_time = NOW() WHERE id = #{stationId}")
    int incrementServiceCount(@Param("stationId") Long stationId);

    /**
     * 更新站点评分
     */
    @Update("UPDATE station SET rating = #{rating}, review_count = #{reviewCount}, updated_time = NOW() WHERE id = #{stationId}")
    int updateStationRating(@Param("stationId") Long stationId, @Param("rating") BigDecimal rating, @Param("reviewCount") Integer reviewCount);

    /**
     * 根据省市区查询站点
     */
    @Select("SELECT * FROM station WHERE province = #{province} AND city = #{city} AND district = #{district} AND deleted = 0")
    List<Station> findByRegion(@Param("province") String province, @Param("city") String city, @Param("district") String district);
}